<?php
/*
	商品独立佣金设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'佣金规则列表'),
	array('op'=>'edit','title'=>'添加/编辑佣金规则'),
);
set_html('tabs',$tabs);
$setting=$this->plugin_setting('setting');
$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['levels']['0']=array('id'=>0,'title'=>'默认等级');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('goods',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','image','price'));
			$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
			//$item['level_id']=$item['level_id']?$this->getcolumn('level',array('id'=>$item['level_id']),'title'):'全部等级';
			if($item['commissions']['commission1']){
				$newcommission=array('isnew'=>1,'level_id'=>$item['level_id']);
				$newcommission=array_merge($item['commissions'],$newcommission);
				$item['commissions']=array();
				$item['commissions'][0]=$newcommission;
				$this->update('goods',array('commissions'=>ijson_encode($item['commissions'])),array('id'=>$id));
			}
			$item['commissions']=$item['commissions'][0];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：<red>平级佣金</red>，是指平级下线佣金收入的%','list'=>array(
			array('name'=>'goods.title','title'=>'商品名称','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'goods.price','title'=>'商品价格','type'=>'string'),
			array('name'=>'ignore','title'=>'是否关闭分销','type'=>'switch','set'=>'set'),
			array('name'=>'commissions.level_id','title'=>'适用分销商等级','type'=>'select','options'=>'levels'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'ignore'=>intval($post['ignore']),
			'level_id'=>intval($post['level_id']),
			'fugourate'=>intval($post['fugourate']),
			'commissions'=>$post['commissions']?ijson_encode($post['commissions']):'',
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->insert('goods',$data);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();	
		if($item['commissions']['commission1']){
			$newcommission=array('isnew'=>1,'level_id'=>$item['level_id']);
			$newcommission=array_merge($item['commissions'],$newcommission);
			$item['commissions']=array();
			$item['commissions'][0]=$newcommission;
			$this->update('goods',array('commissions'=>ijson_encode($item['commissions'])),array('id'=>$id));
		}
	}else{
		$item=array(
			'title'=>'',
			'ignore'=>0,
			'commissions'=>array(),
		);
	}
	$fee_num=$setting['fee_num']?:4;
	$fee_numfast=$fee_num-1;
    $return['item']=$item;
	$commissions=array(
		array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'levels'),
		//array('type'=>'string','name'=>'levelfee','show'=>array('level_id >'=>0),'title'=>'平级分红','help'=>'平级后的分红；填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		//array('type'=>'string','name'=>'maxlevelfee','show'=>array('level_id >'=>0),'title'=>'最大等级分红','help'=>'最大等级后的分红；填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.levelfee','title'=>'平级分红','help'=>'<red>平级佣金</red>，设置--平级佣金基数方式；填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.commission1_0','title'=>'销售奖','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.commission1_2','title'=>'级差奖','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.commission1_1','title'=>'复购奖','help'=>'自身复购奖励（非上级），填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('name'=>'commissions.chaoyue','title'=>'超越奖','type'=>'string'),
		array('name'=>'commissions.chaoyue_sale','title'=>'超越奖(其他下线完成单量)','type'=>'unitstring','unit'=>'笔','help'=>'其他下线完成单量，才能获得超越奖！'),
		//array('name'=>'commissions.quanfee','title'=>'全球业绩奖','type'=>'string','help'=>'拿全平台业绩的%奖励'),
	);
	$htmls=array(
		'type'=>'edit',
		'desc'=>'商品独立佣金规则设置',
		'edit'=>array(
			array('name'=>'goods_id','title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/'),
			//array('type'=>'unitstring','unit'=>'%','title'=>'复购折扣','name'=>'fugourate','help'=>'请填写%折扣'),
			array('name'=>'ignore','title'=>'是否关闭分销','type'=>'switch','help'=>'关闭后，该商品将不参与分销'),
			array('title'=>'[温馨提示]：1、默认等级适合所有等级（当不匹配等级的时候，都按默认等级的比例算！）；2、默认等级，<red>设置的平级，最大级奖励</red>是不能生效的！</br>[温馨提示]：如果基本设置开启了《<red>按(商品佣金设置/等级佣金设置)的字面意思</red>》模式，《直推复购佣金》跟《直推第'.$fee_num.'人起佣金》，不能同时存在，否则，默认生效《直推复购佣金》！<red>平级佣金</red>，是指平级下线佣金收入的%','type'=>'tip'),
			array('type'=>'array','show'=>array('ignore'=>0),'title'=>'佣金设置','name'=>'commissions','add_tip'=>'添加一个设置',
					'list'=>$commissions,
					'details'=>$commissions
			),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->delete('goods',array('id'=>$id));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误','error');
	}
	$post=get_postdata();
	if(isset($post['ignore'])){
		$this->update('goods',array('ignore'=>$post['ignore']),array('id'=>$id));
	
	}
	exi('操作成功！');
}